package org.example.datascreen.service.Impl;

import org.example.datascreen.configuration.YingshiConfig;
import org.example.datascreen.eneity.DTO.CameraDTO;
import org.example.datascreen.eneity.DTO.DeviceDTO;
import org.example.datascreen.eneity.DTO.SensorDataDTO;
import org.example.datascreen.eneity.*;
import org.example.datascreen.repository.*;
import org.example.datascreen.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceRespository deviceRespository;
    @Autowired
    private SoilConductivityDataRespository soilConductivityDataRespository;
    @Autowired
    private SoilNitrogenDataRespository soilNitrogenDataRespository;
    @Autowired
    private SoilTemperatureDataRespository soilTemperatureDataRespository;
    @Autowired
    private SoilMoistureDataRespository soilMoistureDataRespository;
    @Autowired
    private SoilPHDataRespository soilPHDataRespository;
    @Autowired
    private CameraDataRespository cameraDataRespository;
    @Autowired
    private YingshiConfig yingshiConfig;
    @Autowired
    private TokenServiceImpl tokenService;
    @Override
    public List<SensorDataDTO> getConductivityData() {
        List<SoilConductivityData> top7Data = soilConductivityDataRespository.findTop7ByOrderByCreatedTimeDesc();

        return top7Data.stream()
                .sorted((d1, d2) -> d1.getCreatedTime().compareTo(d2.getCreatedTime())) // 按创建时间从小到大排序
                .map(data -> {
                    SensorDataDTO dto = new SensorDataDTO();
                    SimpleDateFormat dateFormat = new SimpleDateFormat("MM.dd", Locale.US);
                    String formattedDate = dateFormat.format(data.getCreatedTime());

                    String[] parts = formattedDate.split("\\.");
                    String month = String.valueOf(Integer.parseInt(parts[0]));
                    String day = String.valueOf(Integer.parseInt(parts[1]));
                    dto.setDate(month + "." + day);
                    dto.setValue(data.getValue().toString());
                    return dto;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<SensorDataDTO> getNitrogenData() {
        List<SoilNitrogenData> top7Data = soilNitrogenDataRespository.findTop7ByOrderByCreatedTimeDesc();

        return top7Data.stream()
                .sorted((d1, d2) -> d1.getCreatedTime().compareTo(d2.getCreatedTime())) // 按创建时间从小到大排序
                .map(data -> {
                    SensorDataDTO dto = new SensorDataDTO();
                    SimpleDateFormat dateFormat = new SimpleDateFormat("MM.dd", Locale.US);
                    String formattedDate = dateFormat.format(data.getCreatedTime());

                    String[] parts = formattedDate.split("\\.");
                    String month = String.valueOf(Integer.parseInt(parts[0]));
                    String day = String.valueOf(Integer.parseInt(parts[1]));
                    dto.setDate(month + "." + day);
                    dto.setValue(data.getValue().toString());
                    return dto;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<SensorDataDTO> getTemperatureData() {
        List<SoilTemperatureData> top7Data = soilTemperatureDataRespository.findTop7ByOrderByCreatedTimeDesc();

        return top7Data.stream()
                .sorted((d1, d2) -> d1.getCreatedTime().compareTo(d2.getCreatedTime())) // 按创建时间从小到大排序
                .map(data -> {
                    SensorDataDTO dto = new SensorDataDTO();
                    SimpleDateFormat dateFormat = new SimpleDateFormat("MM.dd", Locale.US);
                    String formattedDate = dateFormat.format(data.getCreatedTime());

                    String[] parts = formattedDate.split("\\.");
                    String month = String.valueOf(Integer.parseInt(parts[0]));
                    String day = String.valueOf(Integer.parseInt(parts[1]));
                    dto.setDate(month + "." + day);
                    dto.setValue(data.getValue().toString());
                    return dto;
                })
                .collect(Collectors.toList());
    }

    @Override
    public SensorDataDTO getMoistureData() {
        SoilMoistureData data = soilMoistureDataRespository.findFirstByOrderByCreatedTimeDesc();

        SensorDataDTO dto = new SensorDataDTO();
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM.dd", Locale.US);
        String formattedDate = dateFormat.format(data.getCreatedTime());

        String[] parts = formattedDate.split("\\.");
        String month = String.valueOf(Integer.parseInt(parts[0]));
        String day = String.valueOf(Integer.parseInt(parts[1]));
        dto.setDate(month + "." + day);
        dto.setValue(data.getValue().toString());
        return dto;
    }

    @Override
    public SensorDataDTO getPHData() {
        SoilPHData data = soilPHDataRespository.findFirstByOrderByCreatedTimeDesc();

        SensorDataDTO dto = new SensorDataDTO();
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM.dd", Locale.US);
        String formattedDate = dateFormat.format(data.getCreatedTime());

        String[] parts = formattedDate.split("\\.");
        String month = String.valueOf(Integer.parseInt(parts[0]));
        String day = String.valueOf(Integer.parseInt(parts[1]));
        dto.setDate(month + "." + day);
        dto.setValue(data.getValue().toString());
        return dto;
    }

    @Override
    public List<DeviceDTO> getDeviceInfo() {
        List<Device> devices = deviceRespository.findAll();

        return devices.stream()
                .map(device -> {
                    DeviceDTO dto = new DeviceDTO();
                    dto.setName(device.getName());
                    dto.setCategory(device.getCategory());
                    dto.setSerial_number(device.getSerialNumber());
                    dto.setUnit(device.getUnit());
                    return dto;
                })
                .collect(Collectors.toList());
    }

    @Override
    public CameraDTO getCameraInfo() {
        Device camera = deviceRespository.findDeviceByCategory("camera");
        CameraDTO dto = new CameraDTO();
        dto.setMonitor_address(camera.getMonitorAddress());
        if (yingshiConfig.getToken() == null) {
            yingshiConfig.setToken(tokenService.getyingshiToken());
            dto.setToken(yingshiConfig.getToken());
        } else {
            dto.setToken(yingshiConfig.getToken());
        }
        return dto;
    }

    @Override
    public List<String> getPictures() {
        List<String> pictures = cameraDataRespository.findLatest7PhotoURLs();
        return pictures;
    }
}


